//接口

import { Toast } from 'antd-mobile'
// 报错信息 done ！
const errToast = (msg) => {
    Toast.info(`${msg}`, 2)
}
// 用来获取管理员信息的 done !
const getAdmin = (props) => {
    let data
    props.loginReducer
        ? data = props.loginReducer
        : data = JSON.parse(localStorage.getItem('admin'))
    // console.log(data)
    return data
}
//管理员操作
// 管理员登录 done !
const adminLogin = (tel, password, loginTime, props) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/admin/slg'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                admin_phone: `${tel}`, admin_password: `${password}`, time: `${loginTime}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            if (res.code === 40001 || res.code === 40002 || res.code === 40003) {
                errToast(res.msg)
            }
            else {
                errToast(res.msg)
                localStorage.setItem('admin', JSON.stringify(res.data))
                localStorage.setItem('identity', 'admin')
                dispatch({
                    type: 'ADMIN_LOGIN',
                    adminlogin: res.data
                })
                props.history.push('/systemmanage');
            }
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 退出登录 done !
const adminUnlog = () => {
    return (dispatch) => {
        dispatch({
            type: 'ADMIN_UNLOG'
        })
    }
}
//获取管理员数据接口
const adminCheck = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/admin'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then((res) => {
                return res.json()
            })
            .then(res => {
                // console.log(res.data)
                dispatch({
                    type: 'ADMIN_CHECK',
                    adminlist: res.data
                })
            })
    }
}
//获取用户数据接口
const UserCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/user'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_USER',
                    userlist: res.data
                })
            })
    }
}
//获取商家数据接口
const FruiterCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/business/business'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res)
                dispatch({
                    type: 'GET_FRUITER',
                    fruiterlist: res.data
                })
            })
    }
}
//删除管理员   done!
const DelAdmin = (admin_id, admin_id2, admin_password) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/admin'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                admin_id: `${admin_id}`, admin_id2: `${admin_id2}`, admin_password: `${admin_password}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_ADMIN',
                index: admin_id2
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
//添加管理员 done!
const AddAdmin = (admin_id, password, admin_name, admin_position, admin_phone, admin_email, admin_password, password_confim) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/admin'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                admin_id: `${admin_id}`,
                password: `${password}`,
                admin_name: `${admin_name}`,
                admin_position: `${admin_position}`,
                admin_phone: `${admin_phone}`,
                admin_email: `${admin_email}`,
                admin_password: `${admin_password}`,
                password_confim: `${password_confim}`,
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            if (res.code === 40002 || res.code === 40003 || res.code === 40004 || res.code === 40006) {
                errToast(res.msg)
            }
            else {
                errToast(res.msg)
            }
            // dispatch({
            //     type: 'DEL_ADMIN',
            //     index: admin_id
            // })
        }).catch((err) => {
            console.log(err);
        })
    }
}
//动态管理接口（获取文字数据）
const TextCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/dynamic/dynamic'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_TEXT',
                    textlist: res.data.data
                })
            })
    }
}
//动态管理接口（获取图片数据）
const ImgCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/dynamic/dynamic'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_IMG',
                    imglist: res.data.pic
                })
            })
    }
}

//删除发布成功的动态
const DelCommunity = (dynamic_id, admin_id, admin_password) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/dynamic'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                dynamic_id: `${dynamic_id}`, admin_id: `${admin_id}`, admin_password: `${admin_password}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_COMMUNITY',
                index: dynamic_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
//删除用户 done!
const DelUser = (user_id, admin_id, admin_password) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/user'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_id: `${user_id}`, admin_id: `${admin_id}`, admin_password: `${admin_password}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_USER',
                index: user_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
//删除商家
const DelBusiness = (business_id, admin_id, admin_password) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/business'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${business_id}`, admin_id: `${admin_id}`, admin_password: `${admin_password}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_BUSINESS',
                index: business_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
//所有发布的商品数据接口
const GoodsCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res)
                dispatch({
                    type: 'GET_GOODS',
                    goodslist: res.data
                })
            })
    }
}
//所有待审核的商品数据接口
const ExamineCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity/commodity_temp'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_EXAMINE',
                    examinelist: res.data
                })
            })
    }
}
//商品审核成功
const GoodsSuccess = (commodity_temp_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity/commodity'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_temp_id: `${commodity_temp_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'SUCCESS_GOODS',
                index: commodity_temp_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//商品审核失败
const GoodsFail = (commodity_temp_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity/commodity_defate'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_temp_id: `${commodity_temp_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'FAIL_GOODS',
                index: commodity_temp_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//获取反馈数据
const FeedbackCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/feedback'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_FEEDBACK',
                    feedbacklist: res.data.arr1
                })
            })
    }
}

//反馈bug已解决，删除 done！
const DelFeedback = (feedback_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/feedback'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                feedback_id: `${feedback_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_FEEDBACK',
                index: feedback_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//删除商家已发布的商品
const DelGoods = (admin_id, admin_password, commodity_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                admin_id: `${admin_id}`, admin_password: `${admin_password}`, commodity_id: `${commodity_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_GOODS',
                index: commodity_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//获取所有举报数据
const ReportCreator = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/report'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_REPORT',
                    reportlist: res.data.arr1
                })
            })
    }
}

//举报已解决，删除 
const DelReport = (report_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/report'
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                report_id: `${report_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'DEL_REPORT',
                index: report_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
export {
    adminLogin, adminCheck, UserCreator, AddAdmin,
    adminUnlog, getAdmin,
    FruiterCreator, TextCreator, ImgCreator,DelCommunity,
    DelAdmin, DelBusiness, DelUser,
    ExamineCreator, GoodsSuccess, GoodsFail,
    FeedbackCreator, DelFeedback,
    GoodsCreator, DelGoods,
    ReportCreator,DelReport
}   //命名导出


